/*
 * @Author: yongtian.hong
 * @Date: 2018-12-11 20:21:34
 * @LastEditors: yongtian.hong
 * @LastEditTime: 2018-12-25 10:06:17
 * @Description: 基于axios的服务端请求客户端封装，一般用于中间层项目
 */

const axios = require("axios");
const config = require("../app.config.js");
const QS = require("qs");

const http = axios.create({
  baseURL: config.api
});

module.exports = {
  $get: function(url, params, config = {}) {
    return http
      .get(url, Object.assign({ params: params }, config))
      .then(res => {
        return res;
      })
      .catch(err => {
        return err;
      });
  },
  $delete: function(url, params, config = {}) {
    return http
      .delete(url, Object.assign({ params: params }, config))
      .then(res => {
        return res;
      })
      .catch(err => {
        return err;
      });
  },
  $post: function(url, params, config = {}) {
    return http
      .post(url, QS.stringify(params), config)
      .then(res => {
        return res;
      })
      .catch(err => {
        return err;
      });
  },
  $put: function(url, params, config = {}) {
    return http
      .put(url, QS.stringify(params), config)
      .then(res => {
        return res;
      })
      .catch(err => {
        return err;
      });
  },
  $patch: function(url, params, config = {}) {
    return http
      .patch(url, QS.stringify(params), config)
      .then(res => {
        return res;
      })
      .catch(err => {
        return err;
      });
  }
};
